<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;

class CommonController extends Controller
{
    public function get($name = null, $defaultValue = null)
    {
        return Yii::$app->request->get($name, $defaultValue);
    }

    public function post($name = null, $defaultValue = null)
    {
        return Yii::$app->request->post($name, $defaultValue);
    }

    public function errorJson($msg = 'error', $data = [])
    {
        self::jsonOut(0, $msg, $data);
    }

    public function okJson($msg = 'success', $data = [])
    {
        self::jsonOut(1, $msg, $data);
    }

    public function jsonOut($code = 1, $msg = 'success', $data = [])
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        echo json_encode(['code' => $code, 'msg' => $msg, 'data' => $data]);
        return Yii::$app->end();
    }
}